package com.cms.server.resolver.plugins.scsq;

import java.sql.ResultSet;
import java.util.Date;

import com.efounder.builder.base.data.DataSetUtils;
import com.efounder.builder.base.data.EFDataSet;
import com.efounder.builder.base.data.EFRowSet;
import com.efounder.eai.data.JParamObject;
import com.efounder.form.EFFormDataModel;
import com.efounder.form.io.FormDataResolverAdapter;
import com.efounder.form.server.resolver.util.SYS_MDL_VAL;
import com.efounder.sql.JStatement;
import com.metadata.bizmodel.SYS_MODEL;
import com.persistence.FLOW_TASK_LIST;

import dwz.common.util.DateUtil;

public class CMSCheckSubmitToFlow extends FormDataResolverAdapter {

	//初始化FormDataModel
	public void prepareSaveForm(JStatement stmt, EFFormDataModel model, JParamObject PO) throws Exception {		
		String                            strSql = "";
		EFDataSet                     flowNodeDS = null;
		EFRowSet                      nodeRowset = EFRowSet.getInstance();
		EFRowSet                  nextFlowRowset = EFRowSet.getInstance();
		ResultSet                             rs = null;
		EFDataSet                            eds = model.getBillDataSet();
		EFRowSet                             ers = eds.getRowSet(0);
	    String                            F_KJQJ = ers.getString(PO.GetValueByParamName(SYS_MODEL._BLFL_KJQJ_COL_, SYS_MDL_VAL.BILL_KJQJ), "");
	    String                            F_DJBH = ers.getString(PO.GetValueByParamName(SYS_MODEL._BILL_BH_COL_, SYS_MDL_VAL.BILL_BH), null);
	    String                     RESR_IN_CAUSE = PO.GetValueByParamName(FLOW_TASK_LIST._RESR_IN_CAUSE, "");
	    String                           FLOW_ID = PO.GetValueByParamName(FLOW_TASK_LIST._FLOW_ID, "");
	    String                           NODE_ID = PO.GetValueByParamName(FLOW_TASK_LIST._OP_NODE, "");
	    String                           OP_TIME = "";
	    
	    //获取节点信息
		strSql = "select * from FLOW_NODE_LIST where FLOW_ID ='" + FLOW_ID + "' and NODE_ID = '" + NODE_ID + "'";
		rs = stmt.executeQuery(strSql);
		if(rs != null && rs.next()) nodeRowset = (EFRowSet) DataSetUtils.resultSet2RowSet(rs, nodeRowset);
		
		//获取流程节点
		strSql = "select * from FLOW_TASK_LIST where BIZ_DATE = '" + F_KJQJ + "' and BIZ_DJBH = '" + F_DJBH + "' and "
		       + " FLOW_ID = '" + FLOW_ID + "' and NODE_TAG = '" + PO.GetValueByParamName(FLOW_TASK_LIST._OP_NODE, "") + "'";
		//如果是会签节点提交，需要加上TASK_TO_UNIT的条件
		if(nodeRowset.getString("NODE_TYPE", "").equals("countersign")) {
			strSql += " and TASK_TO_UNIT = '" + PO.GetValueByParamName("OP_BMBH", "") + "'";
		}
		strSql += "  order by OP_ID DESC";
		rs = stmt.executeQuery(strSql);
		flowNodeDS = DataSetUtils.resultSet2DataSet(rs, flowNodeDS);
		nodeRowset = flowNodeDS.getRowSet(0);
		
		if(nodeRowset == null) {
			throw new Exception("<br>单据已经结束流程，请重新打开单据！");
		}
		
		if(RESR_IN_CAUSE.equals("submit")) {
			OP_TIME = nodeRowset.getString("OP_TIME", "");
			Date date = new Date();
			date.setTime(Long.parseLong(OP_TIME));
			if(!nodeRowset.getString("RESR_STATUS", "pending").equals("pending")) {
				throw new Exception("<br>单据已经在【" + DateUtil.date2String(date, DateUtil.PATTERN_STANDARD) + "】提交，没有权限提交单据！");
			}
		} else if(RESR_IN_CAUSE.equals("retake")) {
			OP_TIME = nodeRowset.getString("OP_TIME", "");
			Date date = new Date();
			date.setTime(Long.parseLong(OP_TIME));
			if(nodeRowset.getString("RESR_STATUS", "pending").equals("pending")) {
				throw new Exception("<br>单据已经在【" + DateUtil.date2String(date, DateUtil.PATTERN_STANDARD) + "】提交，请重新打开单据！");
			}
		}
		
		strSql = "select * from FLOW_TASK_LIST where BIZ_DATE = '" + F_KJQJ + "' and BIZ_DJBH = '" + F_DJBH + "' and "
		       + " FLOW_ID = '" + FLOW_ID + "' and POP_ID = '" + nodeRowset.getString("OP_ID", "") + "' and RESR_STATUS = 'processed'";
		rs = stmt.executeQuery(strSql);
		flowNodeDS = EFDataSet.getInstance();
		flowNodeDS = DataSetUtils.resultSet2DataSet(rs, flowNodeDS);
		nextFlowRowset = flowNodeDS.getRowSet(0);
	
		if(nextFlowRowset != null && RESR_IN_CAUSE.equals("retake")) {
			OP_TIME = nextFlowRowset.getString("OP_TIME", "");
			Date date = new Date();
			date.setTime(Long.parseLong(OP_TIME));
			if(nextFlowRowset.getString("RESR_STATUS", "pending").equals("processed")) {
				throw new Exception("<br>单据已经在【" + DateUtil.date2String(date, DateUtil.PATTERN_STANDARD) + "】提交，不能取回单据！");
			}
		}
	}
}